<?php


namespace app\common\service;


use PhpOffice\PhpSpreadsheet\IOFactory;

/**
 * excel操作
 * Class ExcelService
 * @package app\common\service
 */
header("Content-type: text/html; charset=utf-8");
class ExcelService
{
    /**
     * 阅读excel
     * 注：将返回一个二维数组
     * @author ligang
     * @param string $inputFileName     文件（含路径）
     * @param int $pIndex               需要操作的Sheet，默认第一个
     * @return array
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @date 2018/8/3 11:06
     */

   public static function readExcel(string $inputFileName, int $pIndex = 0) {
        //自动识别文件后缀
        $inputFileType = IOFactory::identify($inputFileName);
        //创建Reader
        $reader = IOFactory::createReader($inputFileType);
        //只读模式
        $reader->setReadDataOnly(true);
        //加载文件
        $spreadsheet = $reader->load($inputFileName);
        //将文件内容转为数组
        $sheetData = $spreadsheet->getSheet($pIndex)->toArray(null, true, true, true);
        return $sheetData;
    }




}